package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.OrderMapper;
import cn.group.hotel2.mapper.RoomCheckInMapper;
import cn.group.hotel2.mapper.RoomCheckOutMapper;
import cn.group.hotel2.pojo.vo.CostumerStandardVO;
import cn.group.hotel2.pojo.vo.OrderListItemVO;
import cn.group.hotel2.pojo.vo.OrderStandardVO;
import cn.group.hotel2.service.IOrderService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理订单数据相关的业务实现类
 *
 * @author wcl
 * @version 0.0.1
 * @date 2022/11/22
 */
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RoomCheckInMapper roomCheckInMapper;

    @Autowired
    private RoomCheckOutMapper roomCheckOutMapper;

    public OrderServiceImpl() {
        log.debug("创建业务类对象:OrderServiceImpl");
    }

    @Override
    public OrderStandardVO getStandardByOrderNum(String orderNum) {
        log.debug("开始处理【根据订单号查询订单表数据详情】的业务,参数:{}", orderNum);

        OrderStandardVO queryResult = orderMapper.getStandardByOrderNum(orderNum);
        if (queryResult == null) {
            String message = "查询订单详情失败,该订单号不存在";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }

    @Override
    public List<OrderListItemVO> list() {
        return orderMapper.list();
    }

    @Override
    public List<OrderListItemVO> listPage(Integer current) {
        return orderMapper.listPage(current);
    }

    @Override
    public Integer count() {
        return orderMapper.count();
    }

    @Override
    public List<OrderListItemVO> listByStatus(Integer status) {
        return orderMapper.listByStatus(status);
    }

    @Override
    public Integer countByStatus(Integer status) {
        return orderMapper.countByStatus(status);
    }

    @Override
    public void deleteById(Long id) {
        CostumerStandardVO standardByOrderId = roomCheckInMapper.getStandardByOrderId(id);
        CostumerStandardVO standardByOrderId1 = roomCheckOutMapper.getStandardByOrderId(id);
        if (standardByOrderId != null || standardByOrderId1 != null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"有关联的入住或退房登记，禁止删除");
        }
        orderMapper.deleteById(id);
    }


}
